import java.io.*;
import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要复制的文件(绝对or相对)：");
        String word = scanner.next();
        File file = new File(word);
        if(file.isDirectory()){
            System.out.println("输入不是文件");
            return;
        }
        if(!file.isFile()){
            System.out.println("输入文件不是普通文件");
            return;
        }
        System.out.println("输入要复制到的路径：");
        String dest = scanner.next();
        File destFile = new File(dest);
        if(destFile.exists()){
            if(destFile.isDirectory()){
                System.out.println("目标路径已经存在，并且是目录");
                return;
            }
            if(destFile.isFile()){
                System.out.println("目标路径已经存在,是否覆盖：");
                String ans = scanner.next();
                if(!ans.equals("是")){
                    System.out.println("取消复制");
                    return;
                }
            }
        }
        try(InputStream inputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            byte[] buf = new byte[10];
            while (true){
                int n = inputStream.read(buf);
                if(n == -1) break;
                outputStream.write(buf,0,n);
            }
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("复制完成");
    }
}
